package com.baiytfp.hf.HFDataExchange.common.utils;

import java.util.HashMap;
import java.util.Map;

import com.baiytfp.hf.baiytfp.common.db.PageController;
import net.sf.json.JSONObject;

/**
 * <p>
 * Title: Dream
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * 
 * <p>
 * Company: Y X
 * </p>
 * 
 * @author 陈 庭
 * @version 1.0
 */
public class SplitPage {
	private PageController pc = null;
	private String actionTarget = "";

	public SplitPage() {
		pc = new PageController();
	}

	public SplitPage(PageController pc) {
		this.pc = pc;
	}

	// 取得普通分页 HTML 代码
	public String getSplitHtml() {

        StringBuffer htmlStr = new StringBuffer();
        
        htmlStr.append("<form action=\"" + getActionTarget() + "\" method=\"post\" name=\"pageForm\" id=\"pageForm\">");
        	htmlStr.append("<input id='gopage' name='gopage' type='hidden' value='1'/>");
        	htmlStr.append("<div class='pagelist'>");
		        htmlStr.append("<div class='l-btns'> <span>显示</span>");
			        htmlStr.append("<input type='text' class='pagenum' value="+pc.getPageSize()+" disabled>");
			        htmlStr.append("<span>条/页</span>");
			    htmlStr.append("</div>");    
		        htmlStr.append("<div id='PageContent' class='default'>");
		        	htmlStr.append("<span>共"+pc.getTotalRowsAmount()+"记录</span>");
	        	if (pc.isHasPrevious()) {
	        		htmlStr.append("<a href='javascript:' onclick='javascript:goPage(" + pc.getPreviousPage()+ ");'>«上一页</a>");
	        	}else{
	        		htmlStr.append("<span class='disabled'>«上一页</span>");
	        	}
	        	
	        	for(int i=1;i<=pc.getTotalPages();i++ ){
	        		if (pc.getCurrentPage()==i) {
	        			htmlStr.append("<span class='current'>"+i+"</span>");
	        		}else{
	        			htmlStr.append("<a href='javascript:' onclick='goPage("+i+");'>"+i+"</a>");
	        		}
	        	}
	        	//htmlStr.append("<span class='current'>1</span> <a href='javascript:' onclick='page(2,30,\'\');'>2</a> <a href='javascript:' onclick='page(3,30,\'\');'>3</a>");
	        	
	        	if (pc.isHasNext()) {
	        		htmlStr.append("<a href='javascript:' onclick='javascript:goPage(" + pc.getNextPage()+ ");'>下一页»</a>");
	        	}else{
	        		htmlStr.append("<span class='disabled'>下一页»</span>");
	        	}
		        htmlStr.append("</div>");
	        htmlStr.append("</div>");
	    htmlStr.append("</form>");
        
        
        /*htmlStr.append("<td align=\"center\"><!-- 每页显示记录数 : <span onclick='javascript:setUserConfig(30);' style='cursor:pointer'>30</span> | <span onclick='javascript:setUserConfig(50);' style='cursor:pointer'>50</span> | <span onclick='javascript:setUserConfig(100);' style='cursor:pointer'>100</span>--> ");

        htmlStr.append("<form action=\"" + getActionTarget()
                + "\" method=\"post\" name=\"pageForm\" id=\"pageForm\">");
        htmlStr.append("第 " + pc.getCurrentPage() + " 页&nbsp;&nbsp;共"
                + pc.getTotalPages() + "页" + pc.getTotalRowsAmount() + "条信息");
        htmlStr
                .append("&nbsp;&nbsp;&nbsp;&nbsp; <span onclick=\"javascript:goPage('1');\" style=\"cursor:pointer\">首页</span>");
        if (pc.isHasPrevious()) {
            htmlStr.append("  |  <span onclick=\"javascript:goPage('"
                    + pc.getPreviousPage()
                    + "');\" style=\"cursor:pointer\">上一页</span>");
        } else {
            htmlStr.append("  |  <span style=\"color: #999999\">上一页</span>");
        }

        if (pc.isHasNext()) {
            htmlStr.append("  |  <span onclick=\"javascript:goPage('"
                    + pc.getNextPage()
                    + "');\" style=\"cursor:pointer\">下一页</span>");
        } else {
            htmlStr.append("  |  <span style=\"color: #999999\">下一页</span>");
        }

        htmlStr.append("  |  <span onclick=\"javascript:goPage('"
                + pc.getTotalPages()
                + "');\" style=\"cursor:pointer\">尾页</span>");

        htmlStr
                .append("&nbsp;&nbsp; 请输入页码:  <input onkeydown=\"javascript:if(event.keyCode == 13){goPage(this.value);}\" type=\"text\" size=\"3\" name=\"gopage\" id=\"gopage\" value=\"\" class=\"standardInput\" style=\"width:50px;\" />");
        htmlStr
                .append("&nbsp;<input type=\"button\" value=\"转到\" class=\"btn btn-sm\" onclick=\"javascript:goPage(null);\" />");
        // htmlStr.append(" | <span onclick=\"javascript:goPage(null);\"
        // style=\"cursor:pointer\">GO</span>");
        htmlStr.append("<input type=\"hidden\" name=\"rownum\" value=\""
                + pc.getTotalRowsAmount() + "\" />");
        htmlStr.append("<input type=\"hidden\" name=\"currentpage\" value=\""
                + pc.getCurrentPage() + "\" />");

        // 添加附加搜索条件
        String[][] list = pc.getProperty();
        if (list != null && list.length > 0) {
            for (int i = 0; i < list.length; i++) {
                htmlStr.append("<input type=\"hidden\" name=\"" + list[i][0]
                        + "\" value=\"" + list[i][1] + "\" />");
            }
        }

        htmlStr.append("</form>");

        htmlStr.append("</td>");
        htmlStr.append("</tr>");
        htmlStr.append("</table>");*/

        htmlStr.append("<script type=\"text/javascript\">");
        htmlStr.append("function goPage(pageNum){");
        htmlStr.append("    if(pageNum!=''){");
        htmlStr.append("        $('#gopage').val(pageNum)");
        htmlStr.append("    }");
        htmlStr.append("    document.pageForm.submit();");
        htmlStr.append("}");
        htmlStr.append("</script>");

        return htmlStr.toString();
	}
	
	// 取得普通分页 HTML 代码 (备份)
	public String getSplitHtmlBak() {

		StringBuffer htmlStr = new StringBuffer();

		htmlStr
				.append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">");
		htmlStr.append("<tr>");
		htmlStr.append("<td align=\"center\">");

		htmlStr.append("<form action=\"" + getActionTarget()
				+ "\" method=\"post\" name=\"pageForm\" id=\"pageForm\">");
		htmlStr.append("第 " + pc.getCurrentPage() + " 页&nbsp;&nbsp;共"
				+ pc.getTotalPages() + "页" + pc.getTotalRowsAmount() + "条信息");
		htmlStr
				.append("&nbsp;&nbsp;&nbsp;&nbsp; <span onclick=\"javascript:goPage('1');\" style=\"cursor:pointer\">首页</span>");
		if (pc.isHasPrevious()) {
			htmlStr.append("  |  <span onclick=\"javascript:goPage('"
					+ pc.getPreviousPage()
					+ "');\" style=\"cursor:pointer\">上一页</span>");
		} else {
			htmlStr.append("  |  <span style=\"color: #999999\">上一页</span>");
		}

		if (pc.isHasNext()) {
			htmlStr.append("  |  <span onclick=\"javascript:goPage('"
					+ pc.getNextPage()
					+ "');\" style=\"cursor:pointer\">下一页</span>");
		} else {
			htmlStr.append("  |  <span style=\"color: #999999\">下一页</span>");
		}

		htmlStr.append("  |  <span onclick=\"javascript:goPage('"
				+ pc.getTotalPages()
				+ "');\" style=\"cursor:pointer\">尾页</span>");

		htmlStr
				.append("&nbsp;&nbsp; 请输入页码:  <input onkeydown=\"javascript:if(event.keyCode == 13){goPage(this.value);}\" type=\"text\" size=\"3\" name=\"gopage\" id=\"gopage\" value=\"\" class=\"standardInput\" style=\"width:50px;\" />");
		htmlStr
				.append("&nbsp;<input type=\"button\" value=\"提 交\" class=\"standardBtn\" onclick=\"javascript:goPage(null);\" />");
		// htmlStr.append(" | <span onclick=\"javascript:goPage(null);\"
		// style=\"cursor:pointer\">GO</span>");
		htmlStr.append("<input type=\"hidden\" name=\"rownum\" value=\""
				+ pc.getTotalRowsAmount() + "\" />");
		htmlStr.append("<input type=\"hidden\" name=\"currentpage\" value=\""
				+ pc.getCurrentPage() + "\" />");

		// 添加附加搜索条件
		String[][] list = pc.getProperty();
		if (list != null && list.length > 0) {
			for (int i = 0; i < list.length; i++) {
				htmlStr.append("<input type=\"hidden\" name=\"" + list[i][0]
						+ "\" value=\"" + list[i][1] + "\" />");
			}
		}

		htmlStr.append("</form>");

		htmlStr.append("</td>");
		htmlStr.append("</tr>");
		htmlStr.append("</table>");

		htmlStr.append("<script type=\"text/javascript\">");
		htmlStr.append("function goPage(pageNum){");
		htmlStr.append("	if(isSet(pageNum)){");
		htmlStr.append("		$('#gopage').val(pageNum)");
		htmlStr.append("	}");
		htmlStr.append("		if(isNull($('#gopage').val())){");
		htmlStr.append("			alert('没有输入页码');");
		htmlStr.append("			return;");
		htmlStr.append("		}else{");
		htmlStr.append("    		if(!isInt($('#gopage').val())){");
		htmlStr.append("    			alert('页码格式错误，请检查是否为整数');");
		htmlStr.append("    			return;");
		htmlStr.append("    		}else{");
		htmlStr.append("    			var gopage = parseInt($('#gopage').val());");
		htmlStr.append("    			if(gopage < 1 || gopage > " + pc.getTotalPages() + "){");
		htmlStr.append("    				alert('对不起，你输入的页码超出范围，请确认后再提交');");
		htmlStr.append("    				return;");
		htmlStr.append("				}else{");
		htmlStr.append("    				document.pageForm.submit();");
		htmlStr.append("    			}");
		htmlStr.append("    		}");
		htmlStr.append("		}");
		htmlStr.append("	");
		htmlStr.append("}");
		htmlStr.append("</script>");

		return htmlStr.toString();
	}

	// 使用jquery pager 插件分页 , 页面必须加载此插件与 jquery
	public String getPagerSplitHtml() {

		StringBuffer htmlStr = new StringBuffer();

		htmlStr.append("&nbsp;共"
				+ pc.getTotalPages() + "页" + pc.getTotalRowsAmount() + "条信息");
		htmlStr.append("<form action=\"" + getActionTarget()
				+ "\" method=\"post\" name=\"pageForm\"  id=\"pageForm\">");
		

		htmlStr.append("<input type=\"hidden\" name=\"rownum\" value=\""
				+ pc.getTotalRowsAmount() + "\" />");
		htmlStr.append("<input type=\"hidden\" name=\"currentpage\" value=\""
				+ pc.getCurrentPage() + "\" />");
		htmlStr.append("<input type=\"hidden\" name=\"gopage\"  id=\"gopage\" value=\""
				+ pc.getCurrentPage() + "\" />");

		// 添加附加搜索条件
		String[][] list = pc.getProperty();
		if (list != null && list.length > 0) {
			for (int i = 0; i < list.length; i++) {
				htmlStr.append("<input type=\"hidden\" name=\"" + list[i][0]
						+ "\" value=\"" + list[i][1] + "\" />");
			}
		}

		htmlStr.append("</form>");

		htmlStr.append("<script type=\"text/javascript\">");
		htmlStr.append("$(document).ready(function() {");
		htmlStr.append("$(\"#pager\").pager({ pagenumber: " + pc.getCurrentPage() + ", pagecount: "
				+ pc.getTotalPages() + ", buttonClickCallback: PageClick });");
		htmlStr.append("});");
		htmlStr.append("function PageClick(num){");
		htmlStr.append("	if(num != null){");
		htmlStr.append("		$(\"#gopage\").val(num);");
		htmlStr.append("		$(\"#pageForm\").submit();");
		htmlStr.append("	}");
		htmlStr.append("}");
		htmlStr.append("</script>");

		return htmlStr.toString();
	}
	
	// 使用JSON分页 保存分页信息
	public JSONObject getPagerSplitJSON() {
		Map m = new HashMap();
		String[][] prop = pc.getProperty();
		for(int i = 0; i < prop.length; i++){
			m.put(prop[i][0], prop[i][1]);
		}
		
		JSONObject json  = new JSONObject();
		json.put("property", JSONObject.fromObject(m));
		json.put("total_pages", pc.getTotalPages() + "");
		json.put("total_rows", pc.getTotalRowsAmount() + "");
		json.put("action_target", getActionTarget());
		json.put("current_page", pc.getCurrentPage() + "");
		json.put("previousPage", pc.getPreviousPage() + "");
		json.put("nextPage", pc.getNextPage() + "");
		json.put("hasPrevious", pc.isHasPrevious() + "");
		json.put("hasNext", pc.isHasNext() + "");
		json.put("start_row", pc.getPageStartRow());
		
		return json;
	}

	// 取得普通分页 HTML 代码
	public String getZuiPagerSplitHtml() {

		StringBuffer htmlStr = new StringBuffer();
		
		String nextRightBorder = "";
		String nextPagerClass = "";

		//表单信息
		htmlStr.append("<form action=\"" + getActionTarget()
				+ "\" method=\"post\" name=\"pageForm\"  id=\"pageForm\">");
		htmlStr.append("<input type=\"hidden\" name=\"rownum\" value=\""
				+ pc.getTotalRowsAmount() + "\" />");
		htmlStr.append("<input type=\"hidden\" name=\"currentpage\" value=\""
				+ pc.getCurrentPage() + "\" />");
		
		//小于或等于5页时，不需要输入框，直接在下一页做圆角
		if(pc.getTotalPages() > 7){	//只有大于5页时才需要输入页码
			nextRightBorder = " style=\"border-right:none;\" ";
			nextPagerClass = " pager-next ";
		}else{
			htmlStr.append("<input type=\"hidden\" name=\"gopage\" id=\"gopage\" value=\""
					+ pc.getCurrentPage() + "\" />");
		}

		// 添加附加搜索条件
		String[][] list = pc.getProperty();
		if (list != null && list.length > 0) {
			for (int i = 0; i < list.length; i++) {
				htmlStr.append("<input type=\"hidden\" name=\"" + list[i][0]
						+ "\" value=\"" + list[i][1] + "\" />");
			}
		}

		
		//分页器代码
		htmlStr.append("<ul class=\"pager\">");
		if (pc.isHasPrevious()) {
			htmlStr.append("<li class=\"previous\"><a href=\"#\" style=\"border-right:none;\" onclick=\"javascript:goPage('"
					+ pc.getPreviousPage()
					+ "');return false;\">« 上一页</a></li>");
		}else{
			htmlStr.append("<li class=\"previous disabled\"><a href=\"#\" style=\"border-right:none;\" onclick=\"javascript:return false;\">« 上一页</a></li>");
		}
		
		if(pc.getTotalPages() > 7){	//大于5页时才采取计算[左右为首尾页，中间5页]
			//取得中间显示的那页
			int centerPage = pc.getCurrentPage();
			if(pc.getCurrentPage() < 4){	//如果当前页小于4，就一直以4为中心	
				centerPage = 4;
			}
			if(pc.getCurrentPage() + 3 > pc.getTotalPages()){	//如果当前页快接近尾页，则以尾页减3为中心
				centerPage = pc.getTotalPages() - 3;
			}
			
			//中间5页中的左2页
			if(centerPage > 4){	//如果中心页大于4，则表示前面与首页分开了
				//先添加首页
				htmlStr.append(generatePagerNum(1, 1, pc.getCurrentPage()));
				
				htmlStr.append("<li><a href=\"#\" onclick=\"javascript:return false;\">...</a></li>");
				htmlStr.append(generatePagerNum(centerPage - 2, centerPage - 1, pc.getCurrentPage()));
			}else{
				htmlStr.append(generatePagerNum(1, centerPage - 1, pc.getCurrentPage()));
			}
			
			//产生中心页
			htmlStr.append(generatePagerNum(centerPage, centerPage, pc.getCurrentPage()));
			
			//中间5页中的右2页
			if(centerPage + 3 < pc.getTotalPages()){	//如果中心页离尾页超过3页，则表示与尾页分开了
				htmlStr.append(generatePagerNum(centerPage + 1, centerPage + 2, pc.getCurrentPage()));
				htmlStr.append("<li><a href=\"#\" onclick=\"javascript:return false;\">...</a></li>");
				
				//添加尾页
				htmlStr.append(generatePagerNum(pc.getTotalPages(), pc.getTotalPages(), pc.getCurrentPage()));
			}else{
				htmlStr.append(generatePagerNum(centerPage + 1, pc.getTotalPages(), pc.getCurrentPage()));
			}
		}else{
			htmlStr.append(generatePagerNum(1, pc.getTotalPages(), pc.getCurrentPage()));
		}
		
		
		//下一页
		if (pc.isHasNext()) {
			htmlStr.append("<li class=\"next " + nextPagerClass + "\"><a href=\"#\" " + nextRightBorder + " onclick=\"javascript:goPage('"
					+ pc.getNextPage()
					+ "');return false;\">下一页 »</a></li>");
		} else {
			htmlStr.append("<li class=\"next " + nextPagerClass + " disabled\"><a href=\"#\" " + nextRightBorder + " onclick=\"javascript:return false;\">下一页 »</a></li>");
		}
		
		if(pc.getTotalPages() > 7){	//只有大于5页时才需要输入页码
			//页码输入
			htmlStr.append("<li><div class=\"input-group pager-input-group\"><input type=\"text\" name=\"gopage\" id=\"gopage\" class=\"form-control pager-form-control\" value=\"\" size=\"3\" style=\"width:50px;\" onkeydown=\"javascript:if(event.keyCode == 13){goPage(this.value);}\" /><span class=\"input-group-btn pull-left\"><button type=\"button\" class=\"btn\" onclick=\"javascript:goPage(null);return false;\">跳转</button></span></div></li>");
		}
		
		htmlStr.append("</ul>");
		
		htmlStr.append("</form>");
		
		//分页跳转脚本
		htmlStr.append("<script type=\"text/javascript\">");
		htmlStr.append("function goPage(pageNum){");
		htmlStr.append("	if(isSet(pageNum)){");
		htmlStr.append("		$('#gopage').val(pageNum)");
		htmlStr.append("	}");
		htmlStr.append("		if(isNull($('#gopage').val())){");
		htmlStr.append("			alert('没有输入页码');");
		htmlStr.append("			return;");
		htmlStr.append("		}else{");
		htmlStr.append("    		if(!isInt($('#gopage').val())){");
		htmlStr.append("    			alert('页码格式错误，请检查是否为整数');");
		htmlStr.append("    			return;");
		htmlStr.append("    		}else{");
		htmlStr.append("    			var gopage = parseInt($('#gopage').val());");
		htmlStr.append("    			if(gopage < 1 || gopage > " + pc.getTotalPages() + "){");
		htmlStr.append("    				alert('对不起，你输入的页码超出范围，请确认后再提交');");
		htmlStr.append("    				return;");
		htmlStr.append("				}else{");
		htmlStr.append("    				document.pageForm.submit();");
		htmlStr.append("    			}");
		htmlStr.append("    		}");
		htmlStr.append("		}");
		htmlStr.append("	");
		htmlStr.append("}");
		htmlStr.append("</script>");
		
		return htmlStr.toString();
	}
	//循环产生分页的标签
	public String generatePagerNum(int startNum, int endNum, int currentpage) {
		StringBuffer htmlStr = new StringBuffer();
		for(int i = startNum; i <= endNum; i++){
			if(i == currentpage){
				htmlStr.append("<li class=\"active\"><a href=\"#\" onclick=\"javascript:goPage('"
						+ i
						+ "');return false;\">" + i + "</a></li>");
			}else{
				htmlStr.append("<li><a href=\"#\" onclick=\"javascript:goPage('"
						+ i
						+ "');return false;\">" + i + "</a></li>");
			}
		}
		return htmlStr.toString();
	}
	
	public String getActionTarget() {
		return actionTarget;
	}

	public void setActionTarget(String actionTarget) {
		this.actionTarget = actionTarget;
	}
}
